<template>
  <ContentWrap>
    <!--    操作项-->
    <div class="flex flex-items-center my-5">
      <el-button type="primary" @click="handleJump('add')">
        {{ t('logisticsClassification.add') }}
      </el-button>
      <div style="margin-left: 100px; color: red">
        <p>添加说明：</p>
        <p>1、根据仓库管理规则。所有二级实际真实仓库，必须归类到一个特定的一级虚拟仓库。</p>
        <p>2、通过一级虚拟仓的可售国家设置，来限制特定国家/地区的客户下单可选择购买的商品。</p>
        <p
          >3、如某特定商品所在仓库，可售国家逻辑和此处“国家分类”的设置不符。请通过新建一级虚拟仓调整。</p
        >
      </div>
    </div>
    <div class="flex items-center mb-5px" v-if="countryNum">
      <span
        >还有<span class="text-#f56c6c">{{ countryNum }}</span
        >个国家未分类</span
      >
    </div>
    <!-- 表格主体 -->
    <el-table
      :data="dataList"
      highlight-current-row
      stripe
      border
      v-loading="tableLoading"
      :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{ textAlign: 'center' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column :label="t('expandCustomer.serialNumber')" prop="id" min-width="70" />
      <el-table-column
        :label="t('logisticsClassification.cc_name')"
        min-width="250"
        :show-overflow-tooltip="true"
        prop="cc_name"
      />
      <el-table-column
        :label="t('logisticsClassification.sellable_wids')"
        min-width="250"
        :show-overflow-tooltip="true"
        prop="sellable_wids"
      />
      <el-table-column
        :label="t('supply.create_time')"
        min-width="180"
        prop="create_time"
        :show-overflow-tooltip="true"
      />

      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button @click="handleJump('edit', row)" size="small">
            {{ t('action.edit') }}
          </el-button>
          <el-button type="danger" @click="handleDelete('single', row)" size="small">
            {{ t('action.del') }}
          </el-button>
          <el-button type="success" @click="handleJump('detail', row)" size="small">
            {{ t('action.detail') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <Pagination
      :total="paginationTotal"
      v-model:page="form.pageIndex"
      v-model:limit="form.pageSize"
      @pagination="initialize"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import Pagination from '@/components/Pagination/index.vue'
import { ContentWrap } from '@/components/ContentWrap'
import { deleteDataApi, getFieldEnumApi, getListApi } from '@/api/logisticsClassification/index'
import { ElButton, ElMessage, ElMessageBox } from 'element-plus'
import { useClipboard } from '@vueuse/core'

const { copy } = useClipboard()
const { t } = useI18n()
const { push } = useRouter()

const tableLoading = ref<boolean>(false)
const dataList = ref<any[]>([])
const multipleSelection = ref<any[]>([])

const paginationTotal = ref<number>(0)
const form = ref<Record<string, any>>({
  video_title: '',
  start_date: '',
  end_date: '',
  pageIndex: 1,
  pageSize: 20
})

const modelList = ref<Array<Record<string, any>>>([])
const operatorList = ref<Array<Record<string, any>>>([])

const statusList = ref<Array<Record<string, any>>>([
  { label: '对外显示', value: 1 },
  { label: '不对外显示', value: 0 }
])

//格式化时间
const changeTime = (e: Date[]) => {
  if (e && e.length > 0) {
    form.value.start_date = dayjs(e[0]).format('YYYY-MM-DD 00:00:00')
    form.value.end_date = dayjs(e[1]).format('YYYY-MM-DD 23:59:59')
  } else {
    form.value.start_date = null
    form.value.end_date = null
  }
}

const handleCopy = (text: string) => {
  ;(copy(text) as any) && ElMessage.success('复制成功')
}

//批量勾选
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val
}

//点击跳转
const handleJump = (type: string, row?: Record<string, any>) => {
  push({
    path: `/Logistics/Classification/${type}`,
    query: { [(row && 'id') as string]: row?.id }
  })
}

//批量操作
const handleBatch = async (type: string) => {
  const functionMap = {
    delete: () => handleDelete('batch', {})
  }
  if (functionMap[type]) functionMap[type]()
}

//点击删除
const handleDelete = (type: string, row: Record<string, any>) => {
  ElMessageBox.confirm(t('purchase.logistics.confirmDeletion'), t('purchase.logistics.tips'), {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await deleteDataApi({
        id: row.id
      })
      ElMessage.success(t('purchase.logistics.deleteSuccess'))
      await initialize()
    })
    .catch(() => {})
}

//获取枚举
const getDropDownBoxInfo = async () => {
  const resp = await getFieldEnumApi({ field: 'all' })
  modelList.value = resp.data.model
  operatorList.value = resp.data.operator
}

//根据接口枚举匹配出中文名称
const useEnumeratePicker = (
  enumList: any[],
  field: string | number,
  key?: string,
  value?: string
) => {
  if (!enumList) return
  const matchItem =
    enumList.find((item: Record<string, any>) => item[value ?? 'value'] === field) || {}
  return matchItem[key ?? 'label'] || ''
}
const countryNum = ref<number>(0)
//初始化请求
const initialize = async () => {
  tableLoading.value = true
  const params = JSON.parse(JSON.stringify(form.value))
  delete params.date
  const resp = await getListApi(params)
  console.log(resp, '11')
  dataList.value = resp.data.list
  paginationTotal.value = resp.data.total
  countryNum.value = resp.data.countryNum
  tableLoading.value = false
}

//重置查询项
const reset = () => {
  form.value = {
    video_title: '',
    start_date: '',
    end_date: '',
    pageIndex: 1,
    pageSize: 20
  }
  paginationTotal.value = 0
  initialize()
}

onMounted(() => {
  getDropDownBoxInfo()
  initialize()
})
</script>

<style lang="less" scoped></style>
